#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018-09-05 22:33:55
# Project: Dota2

from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('https://www.twitch.tv/directory/game/Dota%202',
                   fetch_type='js',
                   js_script="""
                   function() {
                    window.scrollTo(0,document.body.scrollHeight);
                    }""",
                   callback=self.index_page,
                   validate_cert=False)

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        return {
            "url": response.url,
            "channels": [{
                "title": x('h3').text(),
                "viewers": x('.preview-card-stat').text(),
                "name": x('p > a').text(),
            } for x in response.doc('.stream-thumbnail').items()]
        }
